layui.use(['form','layer','laydate','table','laytpl'],function(){
	    var form = layui.form,
	        layer = parent.layer === undefined ? layui.layer : top.layer,
	        $ = layui.jquery,
	        laydate = layui.laydate,
	        laytpl = layui.laytpl,
	        table = layui.table;
	
	    //公司列表
	    var tableIns = table.render({
	        elem: '#stateList',
	        url : '/comp/getCompList',
	        cellMinWidth : 95,
	        page : true,
	        height : "full-125",
	        limit : 10,
	        limits : [10,15,20,25],
	        id : "stateListTable",
	        cols : [[
	            {field: 'compName', title: '公司名',align:"center"},
	            {field: 'managerName', title: '公司负责人',align:"center"},
	            {field: 'mobilePhone', title: '联系电话',align:"center"},
	            {field: 'des', title: '备注', align:'center'},
	            {field: 'creatTime', title: '注册时间', align:'center'},
	            {title: '操作', width:170, templet:'#stateListBar',fixed:"right",align:"center"}
	        ]]
	    });
	    $(".comp_add").on("click",function(){
	    	add();
	    })
	    
	    function add(){
	    	var index = layui.layer.open({
	    		title : "新增公司",
	    		type : 2,
	    		content : '/comp/editOrAdd/index',
	    		
	    	});
	    	layui.layer.full(index);
	    	//改变窗口大小时，重置弹窗的宽高，防止超出可视区域（如F12调出debug的操作）
	    	$(window).on("resize",function(){
	    		layui.layer.full(index);
	    	});
	    };
	    
	    
	    //编辑
	    function compEdit(edit){
	    	var index = layui.layer.open({
	    		title : "公司操作",
	    		type : 2,
	    		content : '/comp/editOrAdd/index',
	    		success : function(layero, index){
	                var body = layui.layer.getChildFrame('body', index);
	                if(edit){
	                	body.find(".compId").attr("value",edit.compId);
	                    body.find(".compName").val(edit.compName);
	                    body.find(".managerName").val(edit.managerName);
	                    body.find(".mobilePhone").val(edit.mobilePhone);
	                    body.find(".des").val(edit.des);
	                    form.render();
	                };
	            },
	    	})
	    	layui.layer.full(index);
	    	//改变窗口大小时，重置弹窗的宽高，防止超出可视区域（如F12调出debug的操作）
	    	$(window).on("resize",function(){
	    		layui.layer.full(index);
	    	});
	    };
	    
	   
	    
	    //列表操作
	    table.on('tool(stateList)', function(obj){
	        var layEvent = obj.event,
	            data = obj.data;
	
	        if(layEvent === 'edit'){ //编辑
	            compEdit(data);
	        } else if (layEvent === 'del') { // 删除
				layer.confirm('确定删除此公司？', {
					title : '提示信息'
				}, function(index) {
					if (index) {
						$.get("/comp/deleteComp?compId=" + data.compId, function(result) {
							if (result.success) {
								layer.open({
									title : '温馨提示',
									content : "删除成功"
								});
								tableIns.reload();
							} else {
								layer.open({
									title : '温馨提示',
									content : data.message
								});
								tableIns.reload();
							}
						})
					} else {
						tableIns.reload();
					}
	
				});
			}
	    });
	    
	    
	    
	    form.on('submit(compAddOrEdit)',function(data){
	    	// console.log(data.field)
			 $.post("/comp/editOrAddComp",data.field,function(rest){
				 if(rest.success){
					 layer.msg("操作成功");
//					 parent.window.location.reload();
					 var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
					 parent.layer.close(index);
					 parent.layui.table.reload('stateListTable') 
				 }else{
					 layer.msg(rest.message);
				 }
			 })
		})
	})